package edu.fsu.cs.SHPE;

import java.net.ConnectException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Color;
import android.util.Log;


public class SponsorLevelContainer extends ArrayList<SponsorLevel> {
	private static final long serialVersionUID = -6022575526907183895L;

	
	private SponsorLevel tmpSponsorLevel;
	private final static String TAG = "SHPE.SponsorLevelContainer";
	private String tmpString;
	
	public SponsorLevelContainer(String url) throws ConnectException{
		if(!parseXml(url))
			throw new ConnectException();
	}

	public String[] getStrings(){
		String [] strings = new String[this.size()];
		
		for(int x = 0; x < this.size(); x++){
			strings[x] = this.get(x).getName();
			Log.i(TAG, "getting " + strings[x]);
			
		}
		
		return strings;
	}
	
	
	private boolean parseXml(String url){

		Log.i(TAG, "<" + url + ">");

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			// Multiple conference testserver
			//"http://ww2.cs.fsu.edu/~sposaro/test/index.html"

			URL sourceUrl = new URL(url);
			ConferenceXMLHandler conferenceHandler = new ConferenceXMLHandler();
			xr.setContentHandler(conferenceHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));	

		} catch (Exception e) {
			return false;
		}

		return true;

	}

	public class ConferenceXMLHandler extends DefaultHandler {


		public void startDocument() throws SAXException {
			Log.i(TAG, "Document processing started");
			
			tmpSponsorLevel = new SponsorLevel();
			tmpSponsorLevel.setName("All Levels");
			tmpSponsorLevel.setId(0);
			add(tmpSponsorLevel);
			
			tmpSponsorLevel = null;
		}


		public void endDocument() throws SAXException {
			Log.i("HANDLER", "Document processing finished");

			if(tmpSponsorLevel != null)
				add(tmpSponsorLevel);
		}


		public void startElement(String uri, String localName, String qName, Attributes attributes){
			Log.i("HANDLER", "in element " + localName + " qName " + qName + " uri " + uri);


			if(localName.equalsIgnoreCase("conference-sponsorship-level")){

				if(tmpSponsorLevel != null){
					add(tmpSponsorLevel);
				}


				tmpSponsorLevel = null;
				tmpSponsorLevel = new SponsorLevel();

				if(attributes.getValue("name") != null)
					tmpSponsorLevel.setName(attributes.getValue("name"));

				if(attributes.getValue("hexColor") != null)
					tmpSponsorLevel.setColor(Color.parseColor("#" + attributes.getValue("hexColor")));
				
				tmpSponsorLevel.setId(Long.parseLong(attributes.getValue("id")));
			}
			
			if(localName.equalsIgnoreCase("cost")){
				if(attributes.getValue("min") != null)
					tmpSponsorLevel.setMinCost(Float.parseFloat(attributes.getValue("min")));
			}
		}
		
		
		public void characters(char[] ch, int start, int length) throws SAXException{
			tmpString = new String(ch, start, length);
			Log.i(TAG, "string " + tmpString);
		}

		public void endElement(String uri, String localName, String qName)
		throws SAXException {
			Log.i(TAG, "endElement " + localName);

			if(localName == null || tmpString == null || tmpSponsorLevel == null)
				return;


			if(localName.equalsIgnoreCase("level-order"))
				tmpSponsorLevel.setLevelOrder(Long.parseLong(tmpString));
		}
		
	}
	
}
